#include "rational.h"



int Rational::getDivider()
{
	int min = p < q ? p : q;

	for(int i = 2; i <= min; i++)
	{
		if(p%i == 0 && q%i == 0)
		{
			return i;
		}
	}
	return 1;
}

void Rational::Reduse()
{
	while(getDivider() != -1)
	{
		int d = getDivider();
		p /= d;
		q /= d;
	}
}


Rational::Rational(int p, int q)
{
	this->p = p;
	this->q = q;

	Reduse();
}

Rational::Rational(const Rational& rat)
{
	this->p = rat.getP();
	this->q = rat.getQ();

	Reduse();
}

int Rational::getP() const
{
	return p;
}

int Rational::getQ() const
{
	return q;
}

Rational& Rational::operator+=(const Rational& rat)
{
	Rational tmp(rat);
	int new_p = p*rat.getQ() + rat.getP()*q;
	int new_q = q*rat.getQ();

	p = new_p;
	q = new_q;

	Reduse();

	return *this;
}

Rational& Rational::operator-=(const Rational& rat)
{
	Rational tmp(rat);
	int new_p = p*rat.getQ() - rat.getP()*q;
	int new_q = q*rat.getQ();

	p = new_p;
	q = new_q;

	Reduse();

	return *this;
}

Rational& Rational::operator/=(const Rational& rat)
{
	Rational tmp(rat);
	int new_p = p*rat.getQ();
	int new_q = q*rat.getP();

	p = new_p;
	q = new_q;

	Reduse();

	return *this;
}

Rational Rational::operator*=(const Rational& rat)
{
	Rational tmp(rat);
	int new_p = p*rat.getP();
	int new_q = q*rat.getQ();

	p = new_p;
	q = new_q;

	Reduse();

	return *this;
}

Rational::operator double()
{
	double d = p/q;
	return d;
}

Rational Rational::operator++()
{
	p += q;	
	Reduse();	
	return *this;
}

Rational Rational::operator++(int)
{
	Rational rat(*this);
	p += q;	
	Reduse();	
	return rat;
}

Rational Rational::operator--()
{
	p -= q;	
	Reduse();	
	return *this;
}

Rational Rational::operator--(int)
{
	Rational rat(*this);
	p -= q;	
	Reduse();	
	return rat;
}



Rational operator+(const Rational& rat1, const Rational& rat2)
{
	Rational tmp(rat1);
	return tmp+=rat2;
}

Rational operator-(const Rational& rat1, const Rational& rat2)
{
	Rational tmp(rat1);
	return tmp-=rat2;
}

Rational operator/(const Rational& rat1, const Rational& rat2)
{
	Rational tmp(rat1);
	return tmp/=rat2;
}

Rational operator*(const Rational& rat1, const Rational& rat2)
{
	Rational tmp(rat1);
	return tmp*=rat2;
}

bool operator==(const Rational& rat1, const Rational& rat2)
{
	if(rat1.getP() == rat2.getP() && rat1.getQ() == rat2.getQ())
	{
		return true;
	}
	return false;
}

bool operator!=(const Rational& rat1, const Rational& rat2)
{
	return !(rat1 == rat2);
}

bool operator>(const Rational& rat1, const Rational& rat2)
{
	if(rat1.getQ() == rat2.getQ())
	{
		if(rat1.getP() > rat2.getP())
		{
			return true;
		}
	}
	else
	{
		int p1 = rat1.getP()*rat2.getQ();
		int p2 = rat2.getP()*rat1.getQ();

		if(p1 > p2)
		{
			return true;
		}
	}		

	return false;
}

bool operator<(const Rational& rat1, const Rational& rat2)
{
	if(rat1.getQ() == rat2.getQ())
	{
		if(rat1.getP() < rat2.getP())
		{
			return true;
		}
	}
	else
	{
		int p1 = rat1.getP()*rat2.getQ();
		int p2 = rat2.getP()*rat1.getQ();

		if(p1 < p2)
		{
			return true;
		}
	}		

	return false;
}

bool operator>=(const Rational& rat1, const Rational& rat2)
{
	if(rat1 == rat2 || rat1 > rat2)
	{
		return true;
	}
	return false;
}

bool operator<=(const Rational& rat1, const Rational& rat2)
{
	if(rat1 == rat2 || rat1 < rat2)
	{
		return true;
	}
	return false;
}